package cn.edu.cug.cs.gtl.apis.ui.devicetree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.DeviceTreeMouseListener;
import cn.edu.cug.cs.gtl.apis.ui.actions.DeviceTreeSelectionListener;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

public class DeviceTree extends javax.swing.JTree{
    private DefaultMutableTreeNode currentSelectedNode=null;
    public enum NodeType{
        DATA_CLUSTER_NODE,
        STORAGE_CLUSTER_NODE,
        HDFS_CLUSTER_NODE,
        MINIO_CLUSTER_NODE,
        POSTGRESQL_CLUSTER_NODE,
        MYSQL_CLUSTER_NODE,
        ORACLE_CLUSTER_NODE,
        MONGODB_CLUSTER_NODE,

        COMPUTING_CLUSTER_NODE,
        SPARK_CLUSTER_NODE,
        FLINK_CLUSTER_NODE,

        SEARCH_CLUSTER_NODE,
        ELASTICSEARCH_CLUSTER_NODE,
        SOLR_CLUSTER_NODE,
        CACHE_CLUSTER_NODE,
        REDIS_CLUSTER_NODE,
        MEMCACHED_CLUSTER_NODE,
        MESSAGE_CLUSTER_NODE,
        KAFKA_CLUSTER_NODE,
        RABBITMQ_CLUSTER_NODE,
        CONNECTION_NODE
    };

    private DeviceTree() {
    }

    private DeviceTree(Object[] value) {
        super(value);
    }

    private DeviceTree(Vector<?> value) {
        super(value);
    }

    private DeviceTree(Hashtable<?, ?> value) {
        super(value);
    }

    private DeviceTree(TreeNode root) {
        super(root);
    }

    private DeviceTree(TreeNode root, boolean asksAllowsChildren) {
        super(root, asksAllowsChildren);
    }

    private DeviceTree(TreeModel newModel) {
        super(newModel);
    }

    public void setCurrentSelectedNode(DefaultMutableTreeNode node){
        this.currentSelectedNode=node;
    }

    public DefaultMutableTreeNode getCurrentSelectedNode(){
        return this.currentSelectedNode;
    }

    public static DeviceTree create(AbstractApplication theApp){
        ResourceBundle resourceBundle=ResourceBundle.getBundle("uiapi_resource", Locale.getDefault());

        DefaultMutableTreeNode root = new DefaultMutableTreeNode(resourceBundle.getString("data.device.data_cluster"));
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(resourceBundle.getString("data.device.storage_cluster"));
//        DefaultMutableTreeNode node11 = new DefaultMutableTreeNode("HDFS");
//        DefaultMutableTreeNode node12 = new DefaultMutableTreeNode("MinIO");
//        DefaultMutableTreeNode node13 = new DefaultMutableTreeNode("PostgreSQL");
//        DefaultMutableTreeNode node14 = new DefaultMutableTreeNode("MySQL");
//        DefaultMutableTreeNode node15 = new DefaultMutableTreeNode("Oracle");
//        DefaultMutableTreeNode node16 = new DefaultMutableTreeNode("MongoDB");
//        node1.add(node11);
//        node1.add(node12);
//        node1.add(node13);
//        node1.add(node14);
//        node1.add(node15);
//        node1.add(node16);
        String supports = resourceBundle.getString("data.device.storage_cluster.supports");
        String[] ss = supports.split(",");
        for(String s:ss){
            node1.add(new DefaultMutableTreeNode(s));
        }

        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(resourceBundle.getString("data.device.computing_cluster"));
//        DefaultMutableTreeNode node21 = new DefaultMutableTreeNode("Spark");
//        DefaultMutableTreeNode node22 = new DefaultMutableTreeNode("Flink");
//        node2.add(node21);
//        node2.add(node22);
        supports = resourceBundle.getString("data.device.computing_cluster.supports");
        ss = supports.split(",");
        for(String s:ss){
            node2.add(new DefaultMutableTreeNode(s));
        }
        DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(resourceBundle.getString("data.device.search_cluster"));
//        DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("ElasticSearch");
//        DefaultMutableTreeNode node32 = new DefaultMutableTreeNode("Apache Solr");
//        node3.add(node31);
//        node3.add(node32);
        supports = resourceBundle.getString("data.device.search_cluster.supports");
        ss = supports.split(",");
        for(String s:ss){
            node3.add(new DefaultMutableTreeNode(s));
        }
        DefaultMutableTreeNode node4 = new DefaultMutableTreeNode(resourceBundle.getString("data.device.message_cluster"));
//        DefaultMutableTreeNode node41 = new DefaultMutableTreeNode("Kafka");
//        DefaultMutableTreeNode node42 = new DefaultMutableTreeNode("RabbitMQ");
//        node4.add(node41);
//        node4.add(node42);
        supports = resourceBundle.getString("data.device.message_cluster.supports");
        ss = supports.split(",");
        for(String s:ss){
            node4.add(new DefaultMutableTreeNode(s));
        }
        DefaultMutableTreeNode node5 = new DefaultMutableTreeNode(resourceBundle.getString("data.device.cache_cluster"));
//        DefaultMutableTreeNode node51 = new DefaultMutableTreeNode("Redis");
//        DefaultMutableTreeNode node52 = new DefaultMutableTreeNode("Memcached");
//        node5.add(node51);
//        node5.add(node52);
        supports = resourceBundle.getString("data.device.cache_cluster.supports");
        ss = supports.split(",");
        for(String s:ss){
            node5.add(new DefaultMutableTreeNode(s));
        }
        root.add(node1);
        root.add(node2);
        root.add(node3);
        root.add(node4);
        root.add(node5);
        DeviceTree tree = new DeviceTree(root);
        tree.addMouseListener(new DeviceTreeMouseListener(theApp));
        tree.addTreeSelectionListener(new DeviceTreeSelectionListener(theApp));
        return tree;
    }

    public static NodeType getNodeType(DefaultMutableTreeNode node){
        return NodeType.CONNECTION_NODE;
    }

    public static boolean isConnectionNode(DefaultMutableTreeNode node){
        return true;
    }



    public static JPopupMenu createPopupMenuForConnectionNode(){
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Add");
        popupMenu.add(menuItem);
        menuItem = new JMenuItem("Delete");
        popupMenu.add(menuItem);
        menuItem = new JMenuItem("Rename");
        popupMenu.add(menuItem);
        return popupMenu;
    }
    public static JPopupMenu createPopupMenuForNonConnectionNode(){
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Add");
        popupMenu.add(menuItem);
        return popupMenu;
    }
}
